import requests
import json
from dbUtil import db
headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
    ,"referer":"https://www.zhipin.com/web/geek/jobs?query=%E8%BF%90%E7%BB%B4&city=1012401"
    ,"cookie":"lastCity=101240100; ab_guid=72082a10-56b3-4fc3-8e6a-e5c2162e6f6b; __g=-; __l=l=%2Fwww.zhipin.com%2Fweb%2Fgeek%2Fjobs%3Fquery%3D%25E8%25BF%2590%25E7%25BB%25B4%26city%3D101240100&r=&g=&s=3&friend_source=0; __zp_stoken__=3f39fOjfDmcK7wqHCvToxAwMLDQQ0MEE3Kjk4OiMyQDQ6N0A%2BMjo3OCBAKkrCvhBPKFzDi8O8w4A7IkBAODo3MkA7NxJANMWBw4E2QRxZwrwHTCJYw5FZwqXCuywfCMO6wrsMY8K3D1wvLBLDgDtAOTQDwr%2FDgMK4PMK%2FwrvCuTnCuMK7wrJAQTQ7JTwHDQgPPEFDSl4OT19LWFhDAkJCSS80NEE7wpHCoSY%2BCwoHEBAMDQgPDwYHChAQEBEECwsKCwYRESM6wprCu0lZw6jCgcOtxKHCocKcw6vCp8K0XcKkwqjCosK3wqpDwqtowpNMwpNWwq3CqlTCucKCRsKgwrbCnWFdwrTCskxIW8K%2FesKudUtEYcK6S2gFfwJcBD8IUMOGw4o%3D; __c=1746365728; __a=37835918.1746365728..1746365728.4.1.4.4"
}
job = input("请输入查询的工作:")
citya = input("请输入查询的地点:")
cityDic = {"全国":"100010000","南昌":"101240100","北京":"101010100",
           "上海":"101020100","广州":"101280100","深圳":"101280600",
"杭州":"101210100","天津":"101030100","西安":"101110100",
"苏州":"101190400","武汉":"101200100","厦门":"101230200",
"长沙":"101250100","成都":"101270100","郑州":"101180100"
           }
city = cityDic.get(citya,"100010000")
url = "https://www.zhipin.com/wapi/zpgeek/search/joblist.json?scene=1&query="+job+"&city="+city+"&experience=&payType=&partTime=&degree=&industry=&scale=&stage=&position=&jobType=&salary=&multiBusinessDistrict=&multiSubway=&page=1&pageSize=100&_=1741750103962"
resp = requests.get(url, headers=headers)
text = json.loads(resp.content.decode())
print(text)
# 获取数据
data = text["zpData"]["jobList"]
# 获取操作数据的对象
cursor = db.cursor()
for i in data:
    sql = '''
        insert into s_job values
        (null,'%s','%s','%s','%s','%s','%s')
    '''%(i["cityName"],i["brandName"],i["jobName"],i["jobDegree"],
         i["jobExperience"],i["salaryDesc"])
    try:
        cursor.execute(sql) # 执行sql
        db.commit() #提交数据
    except:
        print("数据有问题")
        db.rollback()